package com.jck.wx.web;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.jck.config.IdcardConfig;
import com.jck.dplus.common.annomapper.MybatisBaseDaoCRUD;
import com.jck.dplus.common.utils.IPUtils;
import com.jck.dplus.common.utils.util.ChkUtil;
import com.jck.dplus.common.utils.util.Times;
import com.jck.wx.util.FileClass;
import com.jck.wx.util.IdcardUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/idcard")
public class IdcardController {
	@Autowired
	private IdcardConfig idcardConfig;

	@Autowired
	public MybatisBaseDaoCRUD mBaseDao;


	@GetMapping("/{token}/testidcard")
	public Map idcard(@PathVariable String token,HttpServletRequest request) {
		Map map = new HashMap();
		log.info(Times.getTimes()+":ip-"+IPUtils.getIpAddr(request));
		if(!idcardConfig.getTOKEN().equals(token)) {
			map.put("code",1);
			map.put("des", "fail");
			return map;
		}
		try {
			String result=IdcardUtil.getJsonString(idcardConfig.getWEBOCRURL(), idcardConfig.getENGINETYPE(), idcardConfig.getHEADPORTRAIT(), idcardConfig.getIDNUMBERIMAGE(), idcardConfig.getAPPID(), idcardConfig.getAPIKEY(), "d:/1.jpg");
			map=JSON.parseObject(result);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code",1);
			map.put("des", "fail");
		}
		return map;
	}
	@PostMapping("/{token}/idcardByImageBase64")
	public Map idcard(@PathVariable String token,@RequestParam String imageBase64,HttpServletRequest request) {
		log.info(Times.getTimes()+":ip-"+IPUtils.getIpAddr(request));
		Map map = new HashMap();
		if(!idcardConfig.getTOKEN().equals(token)) {
			map.put("code",1);
			map.put("des", "fail");
			return map;
		}
		try {
			String result=IdcardUtil.getJsonString(idcardConfig.getWEBOCRURL(), idcardConfig.getENGINETYPE(), idcardConfig.getHEADPORTRAIT(), idcardConfig.getIDNUMBERIMAGE(), idcardConfig.getAPPID(), idcardConfig.getAPIKEY(), imageBase64,true);
			map=JSON.parseObject(result);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code",1);
			map.put("des", "fail");
		}
		return map;
	}
	@PostMapping("/{token}/idcardByImageFile")
	public Map idcard(@PathVariable String token,@RequestParam(value = "image",required = true) MultipartFile file,HttpServletRequest request) {
		log.info(Times.getTimes()+":ip-"+IPUtils.getIpAddr(request));
		Map map = new HashMap();
		if(!idcardConfig.getTOKEN().equals(token)) {
			map.put("code",1);
			map.put("des", "fail");
			return map;
		}
		String type=file.getContentType();
		if(type.contains("jpeg")||type.contains("png")) {
			try {
				String result=IdcardUtil.getJsonString(idcardConfig.getWEBOCRURL(), idcardConfig.getENGINETYPE(), idcardConfig.getHEADPORTRAIT(), idcardConfig.getIDNUMBERIMAGE(), idcardConfig.getAPPID(), idcardConfig.getAPIKEY(), file.getBytes());
				map=JSON.parseObject(result);
				} catch (Exception e) {
					e.printStackTrace();
					map.put("code",1);
					map.put("des", "fail");
				}
		}else {
			map.put("code",1);
			map.put("des", "fail");
			return map;
		}
		
		return map;
	}

}